var util = require("../../../utils/util.js"),
{api} = require("../../../utils/api.js"),
    app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageSize: 10,
        page: 1,
        list: [],
        proScroll: false,
        showEmpty: false,
        activeId:''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.patientList();
    },

    async patientList() {
        let {
            data: {
                data,
                total
            }
        } = await api.patientList({
            page: this.data.page,
            pageSize: this.data.pageSize
        });
        let listArr = data && Array.isArray(data) ? data : [];
        this.setData({
            proScroll: listArr.length < this.data.pageSize ? true : false,
            list: this.data.page == 1 ? listArr : [...this.data.list, ...listArr],
            showEmpty: total == 0 ? true : false
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        if (!this.data.proScroll) {
            let page = this.data.page;
            page = page + 1;
            this.setData({
                page: page
            })
            this.patientList();
        }
    },
    addMethod() {
        // wx.navigateTo({
        //     url: '/pages/mine/thePatientAdd/thePatientAdd',
        // })
        if(!this.data.activeId || this.data.activeId == '')return wx.showToast({title:'请选择一张就诊卡',icon:'none'})
        api.changePatient({patient_id:this.data.activeId}).then(res=>{
            wx.showToast({
              title: '操作成功',
              icon:'success'
            })
            this.setData({activeId:''})
            this.setData({page: 1})
            this.patientList();
        })
    },
    setIsMin(e){
        let {id,status}=e.currentTarget.dataset;
        if(status == 0){
            this.setData({activeId:id})
        }
    },
    edit(e){
        let {id,isme}=e.currentTarget.dataset;
        let query='?id='+id;
        if(isme == 1)query=query+'&is_me=1'
        wx.navigateTo({
            url: '/pages/mine/thePatientAdd/thePatientAdd'+query,
        })
    },
    del(){
        let {id}=e.currentTarget.dataset,that=this;
        wx.showModal({
          title: '提示',
          content: '是否要删除此项数据?',
          complete: async (res) => {
            if (res.confirm) {
              const {code}=await api.patientDel(id);
              wx.showToast({
                title: '操作成功',
                icon:'success'
              });
              that.setData({page:1})
              that.patientList();
            }
          }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(e) {
        return util.shareEvent(e)
      }
})